function plugins(cmd, arg)
%
% create, and maintain a plugin menu list.
% plugins are .m files that can be listed and executed from the menu
% they exist in the plugin directory (below the current directory)
% and therefore are configurable by the user. 
% Modified from the "macro" ones I created for the ACQ program
% 5/25/02 Paul B. Manis
%
% the main routine is simply a dispatch routine to the several functions
% that are associated with the plugins...

% commands are as follows:
% 1. 	plugins('create') creates a menu entry called Plugins, and
% 		populates the drop-down list with the m files found in the plugins directory
% 		this occurs in the current window..... possiblity exists for mulitple definitions,
% 		so be careful.
% 2. 	plugins('update') updates the current plugin list.
% 3. 	plugins('execute', pluginame) executes the plugin selected in the drop down list
%

switch(cmd)
case 'create'
	f5 = uimenu('Label', '&Plugins',  'tag', 'Plugin_Menu');
	uimenu(f5, 'Label', '&Update list', 'Callback', 'plugins(''update'');'); % place update command in list
   plugins('update');
   
case 'update'
   plugin_update;
   
case 'execute'
   if(nargin < 2)
      QueMessage('Plugins: execution requires argument', 1);
      return;
   end;
   plugin_execute(arg);
   
otherwise
   QueMessage(sprintf('Plugins: Bad command: %s\n', cmd));
   return;
end;
return;


%-----------------------------------------------------------------------


function plugin_update()

global PATHS

%hfig = findobj('Tag', 'Plugin_Menu');
% delete any 'plugin_' tagged references
%h = findobj;
%v = get(h, 'Tag');
%p = strmatch('plugin_', v); % find all plugin files.
%if(~isempty(p))
%   delete(h(p)); % remove them...
%end;
pdir = [PATHS.BP slash4OS('\Plugins')];% find the plugins in the directory
if(exist(pdir, 'dir'))
   all_plugins = dir([pdir slash4OS('\*.m')]);
else
   QueMessage(sprintf('Plugin_update: invalid path'), 1);
   return;
end;
resource=strfind({all_plugins.name}, '._');
rs = find([resource{:}] == 1); % which are the resource files?
rl = 1:length(resource);
all_plugins = all_plugins(setxor(rl, rs));
QueMessage(sprintf('Found %d Plugins', length(all_plugins)), 1);
pnc = {all_plugins.name};
pn = sort(pnc); % sort alphabetically
%
% create a temporary figure for the plugins - this is just a window with a
% lot of buttons, dynamically generated..
%if(exist('plugin_bar.fig', 'file'))
%    hfig = openfig('plugin_bar.fig', 'reuse', 'invisible');
%    if(!isempty(hfig))
%        figure(hfig);
%    end;
%else
%
%end;
hfig = findobj('tag', 'Plugins');
if(isempty(hfig))
    hfig  = figure('tag', 'Plugins');
else
    figure(hfig);
    clf;
end;

set(hfig, 'Name', 'Plugin List', 'numbertitle', 'off', 'tag', 'Plugins');
xs = 150;
ys = 25;
xsp = 25;
ysp = 2;

maxinrow = 16;
row = 0;
col = 1;
% precalculate the window size needed to hold all these
maxx = 200;
maxy = 100;
for i = 1:length(all_plugins)
    row = row + 1;
    if(row > maxinrow)
        row = 1;
        col = col + 1;
    end;

    xp(i) = (col-1) * xs + 25;
    yp(i) = (row-1) * ys + 2;
    if(xp(i) > maxx)
        maxx = xp(i)+xs+xsp;
    end;
    if(yp(i) > maxy)
        maxy = yp(i) + ys+ysp;
    end;
end;
set(hfig, 'position', [100, 100, maxx, maxy+50], 'visible', 'on');

for i = 1:length(all_plugins)
   if(i == 1)
      sep = 'On';
   else
      sep = 'Off';
   end;
   [p cb e] = fileparts(pn{i});
   %cb = ['Macros\' cb];
%   uimenu(hfig, 'Label', pn{i}, 'Callback', sprintf('plugins(''execute'', ''%s'');', cb), 'Tag', sprintf('plugin_%d', i), ...
%      'Separator', sep); % get the protocol if selected...

uicontrol(hfig, 'style', 'pushbutton', 'string', pn{i}, 'Callback', sprintf('plugins(''execute'', ''%s'');', ...
       cb), 'Tag', sprintf('plugin_%d', i), ...
       'Position', [xp(i), maxy-yp(i), xs, ys]); % get the protocol if selected...

end;   
% saveas(hfig, 'plugins/plugin_bar.fig');
set(hfig, 'visible', 'on');

%-----------------------------------------------------------------------

function plugin_execute(plugin_file)
%

cdir = pwd;
switch(computer)
    case 'PCWIN'
        plugdir = 'c:\mat_datac\plugins';
    case 'MAC'
        plugdir = '/Users/pmanis/Desktop/mat_datac/Plugins';
    case 'MACI'
        plugdir = [pwd '/plugins'];
 %       disp 'maci'
    case 'LNX86'
        plugdir = '/home/pmanis/mat_datac/plugins';
    otherwise
end;

test=findstr('plugins', cdir);
if(isempty(test) || test == 0)
    olddir = cd(plugdir);
else
    olddir = pwd; % 'c:\mat_datac'; 
end;
exist(plugin_file) 
try
   feval(plugin_file)
catch
   QueMessage(sprintf('Plugin <%s> failed?', plugin_file), 1);
end;
cd(olddir);
return;
